﻿Imports Training.Common

Partial Public Class EditQuizQuestAns
    Inherits System.Web.UI.Page
    Dim qid As Integer 'quizid
    Dim qsid As Integer 'questionid
    Dim qs As String ' question
    Dim aid As Integer ' answerid

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ' check for session values
        If IsNothing(Session("qid")) Or IsNothing(Session("qnm")) Or IsNothing(Session("qst")) Or IsNothing(Session("qor")) Then
            If Not IsNothing(Request.QueryString("qid")) Or IsNothing(Request.QueryString("qsid")) Or IsNothing(Request.QueryString("qnm")) Or IsNothing(Request.QueryString("qst")) Or IsNothing(Request.QueryString("qor")) Then
                ' set session variables
                Session("qid") = Request.QueryString("qid")
                Session("qsid") = Request.QueryString("qsid")
                Session("qnm") = Request.QueryString("qnm")
                Session("qst") = Request.QueryString("qst")
                Session("qor") = Request.QueryString("qor")

                lblQuiz.Text = Session("qnm") ' quiz name
                lblQuest.Text = Session("qst")
            Else
                Response.Redirect("QuizControl.aspx")
            End If
            'Response.Redirect("QuizControl.aspx")
        Else
            ' check if an answer delete request was received
            If Not IsNothing(Request.QueryString("aid")) Then
                If Not Page.IsPostBack Then
                    aid = Request.QueryString("aid")
                    ' delete the answer
                    DeleteAnswer(aid)
                End If
            End If
            lblQuiz.Text = Session("qnm") ' quiz name
            lblQuest.Text = Session("qst") ' question name
        End If
    End Sub

    ''' <summary>
    ''' Create link to Delete quiz answer
    ''' </summary>
    ''' <param name="anid"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function DeleteLink(ByVal anid As Integer) As String
        Dim retVal As String
        retVal = "<a href=""EditQuizQuestAns.aspx?aid=" & anid & """>delete</a>"

        Return retVal
    End Function

    ''' <summary>
    ''' Delete the answer
    ''' </summary>
    ''' <param name="aid"></param>
    ''' <remarks></remarks>
    Private Sub DeleteAnswer(ByVal aid As Integer)
        DeleteQuizAnsQuesXRef(Session("qid"), Session("qsid"), aid)
        dsQuestionAnswers.DataBind()
        gvAnswers.DataBind()
    End Sub

    ''' <summary>
    ''' Insert New Question/Answer combination
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub AddQuizQuestion()
        Dim qid As Integer = Session("qid")
        Dim qsid As Integer = Session("qsid")
        Dim aid As Integer = ddlAnswer.SelectedValue
        Dim ca As Boolean
        Dim aod As Integer = tbOrder.Text
        Dim qod As Integer = Session("qor")

        ' check that answer order is in correct format
        If Not IsNumeric(aod) Then
            lblMsg.Text = "Answer Order MUST be numeric"
            Exit Sub
        End If

        ' detemine correct answer
        If ddlCorrect.SelectedValue = 1 Then
            ca = True
        Else
            ca = False
        End If

        If InsertQuizQAXRef(qid, qsid, aid, ca, aod, qod) <> 0 Then
            lblMsg.Text = "Error inserting Question/Answer. Please try again."
        End If
    End Sub

    ''' <summary>
    ''' Reset form fields to defaults so use can add another answer
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub ResetFields()
        ddlAnswer.SelectedValue = "-1"
        tbOrder.Text = String.Empty
        ddlCorrect.SelectedValue = "-1"
    End Sub

    ''' <summary>
    ''' Complete button click event
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnComplete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnComplete.Click
        Response.Redirect("AddEditQuizQuestions.aspx?qid=" & Session("qid") & "&qnm=" & Session("qnm"))
    End Sub

    ''' <summary>
    ''' Continue button click event
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnContinue_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnContinue.Click
        AddQuizQuestion()
        ResetFields()
        dsQuestionAnswers.DataBind()
        gvAnswers.DataBind()
    End Sub
End Class